<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18/0018
 * Time: 14:44
 */

namespace app\sports_admin\controller;


use app\exception\ExpensesReceiptsException;
use think\Request;
use app\sports_admin\model\ExpensesReceipts as ExpensesReceiptsModel;
use app\sports_admin\service\ExpensesReceipts as ExpensesReceiptsService;
class ExpensesReceipts extends Base
{
    //收支首页
    public function index()
    {
        return view('index');
    }
    /**
     * 获取收支信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 备份的开始时间
     * @param $end 备份的结束时间
     * @param $type 收支类型
     * @param $status 收支状态
     */
    public function getExpensesReceiptsInfo()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','start','end','type','status']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $start = trim($params['start']);
        $end = trim($params['end']);
        $type = trim($params['type']);
        $status = trim($params['status']);
        return ExpensesReceiptsModel::getExpensesReceiptsInfo($page,$rows,$sort,$sortOrder,$searchName,$start,$end,$type,$status);
    }
    //添加收支单页面
    public function add()
    {
        return view('add');
    }
    //添加收支单
    public function insert(Request $request)
    {
        if($request->isAjax()){
            return ExpensesReceiptsService::insert();
        }
    }
    /*
     * 编辑收支单页面
     * @param $id 收支单id
     */
    public function edit()
    {
        $id = intval(input('get.id'));
        $info = ExpensesReceiptsModel::findInfo($id);
        return view('edit',compact('info'));
    }
    /*
     * 修改收支单信息
     */
    public function update(Request $request)
    {
        if($request->isAjax()){
            return ExpensesReceiptsService::update();
        }
    }
    /*
     * 审核通过
     */
    public function adopt(Request $request)
    {
        if($request->isAjax()){
            return ExpensesReceiptsService::adopt();
        }
    }
    /*
     * 删除收支单
     * @param $id id号
     */
    public function delete(Request $request)
    {
        if($request->isAjax()){
            $id = intval(input('post.id'));
            $bool = ExpensesReceiptsModel::destroy($id);
            if(!$bool){
                throw new ExpensesReceiptsException([
                    'errorCode'=>400,
                    'msg'=>'删除失败'
                ]);
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }
    }
}